let  num = 0
const workerList = [] // 存储所有worker

// 创建一个共享的worker

self.addEventListener('connect', (event) => {
  const port = event.ports[0]
  workerList.push(port)

  port.postMessage(num) // 从初始化

//   监听动作
  port.onmessage = (event) => {
    if(event.data === 'add'){
        num++
      }else{
        num--
      }
      // 向所有worker发送消息
      workerList.forEach(item => {
        item.postMessage(num)
      })
  }


})